package com.ssm.service.impl;

import com.ssm.dao.ScoreMapper;
import com.ssm.entity.Score;
import com.ssm.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 成绩服务实现类
 */
@Service
public class ScoreServiceImpl implements ScoreService {

    @Autowired
    private ScoreMapper scoreMapper;

    @Override
    @Cacheable(value = "scoreList", key = "'all'")
    public List<Score> findAll() {
        return scoreMapper.selectAll();
    }

    @Override
    @Cacheable(value = "score", key = "#id")
    public Score findById(Integer id) {
        return scoreMapper.selectById(id);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"scoreList", "score"}, allEntries = true)
    public boolean add(Score score) {
        return scoreMapper.insert(score) > 0;
    }

    @Override
    @Transactional
    @CacheEvict(value = {"scoreList", "score"}, allEntries = true)
    public boolean update(Score score) {
        return scoreMapper.update(score) > 0;
    }

    @Override
    @Transactional
    @CacheEvict(value = {"scoreList", "score"}, allEntries = true)
    public boolean delete(Integer id) {
        return scoreMapper.deleteById(id) > 0;
    }

    @Override
    @Cacheable(value = "scoreList", key = "'student:' + #studentId")
    public List<Score> findByStudentId(Integer studentId) {
        return scoreMapper.selectByStudentId(studentId);
    }

    @Override
    @Cacheable(value = "scoreList", key = "'course:' + #courseId")
    public List<Score> findByCourseId(Integer courseId) {
        return scoreMapper.selectByCourseId(courseId);
    }
} 